package com.bawei.base.mvvm

import android.os.Bundle
import android.view.LayoutInflater
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import java.lang.reflect.ParameterizedType

open abstract class BaseActivity<VDB : ViewDataBinding, VM : BaseViewModel> : AppCompatActivity() {
    private val type by lazy{
        (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments
    }
    protected val binding by lazy {
        val clazz = type[0] as Class<VDB>
        clazz.getMethod("inflate",LayoutInflater::class.java).
                invoke(null,layoutInflater)as VDB
    }
        protected val viewModel by lazy {
            val clazz = type[1] as Class<VM>
            ViewModelProvider(this)[clazz]
        }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initView()
        initData()
    }

    abstract fun initView()

    abstract fun initData()
}